<!doctype html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no" />
	<title>首页</title>
	<link rel="stylesheet" type="text/css" href="../c/animate.css" />
	<link rel="stylesheet" type="text/css" href="../c/aui.css" />
	<style>
		html {
			/*height: auto;*/
		}

		body {
			position: relative;
			height: 100%;
			background: url('../i/login.png');
			background-size: cover;
		}

		.sx-content {
			margin: 0 0.75rem;
			color: #fff;
		}

		.sx-h {
			width: 3rem;
			display: inline-block;
			/*height: 3rem;*/
			margin-left: -0.75rem;
		}

		.sx-h img {
			width: 100%;
			padding: 1rem;
		}

		.i {
			margin-top: 2.5rem;
		}

		.i p {
			text-align: center;
			font-size: 0.85rem;
			color: #fff;
			margin-bottom: 1rem;
		}

		.t input {
			position: relative;
			padding: 0.75rem;
			border-radius: 0.15rem;
			font-size: 0.95rem;
			height: auto;
			background: rgba(226, 226, 226, 0.2);
			color: #fff;
			letter-spacing: 1px;
		}

		.t #yzm {
			margin-right: 8rem;
		}

		.t #yzm input {
			border-top-right-radius: 0rem;
			border-bottom-right-radius: 0rem;
		}

		.t div {
			margin-top: 0.5rem;
			position: relative;
		}

		.t {
			margin: 0 1rem;
		}

		input::-webkit-input-placeholder {
			color: rgba(226, 226, 226, 0.5);
			letter-spacing: 1px;
			font-size: 0.85rem;
		}

		.t div span {
			position: absolute;
			right: 0;
			top: 0;
			display: block;
			background: rgba(226, 226, 226, 0.2);
			padding: 0.75rem;
			border-top-right-radius: 0.15rem;
			border-bottom-right-radius: 0.15rem;
			font-size: 0.7rem;
			height: auto;
			color: #fff;
			letter-spacing: 1px;
			margin-right: -8rem;
			color: #999;
			width: 7.5rem;
			text-align: center;
		}

		.s h4 {
			text-align: center;
			opacity: 0.8;
			font-size: 0.75rem;
		}

		.s {
			width: 80%;
			margin: 0 auto;
			margin-top: 3rem;
		}

		.s .aui-card-list-footer {
			margin-top: 0.75rem;
			text-align: center;
		}

		.s .aui-card-list-footer div {
			width: 2.5rem;
		}

		.s .aui-card-list-footer p {
			color: #fff;
			font-size: 0.7rem;
			padding-top: 0.25rem;
		}

		.sx-xieyi {
			text-align: center;
			color: #fff;
			font-size: 0.6rem;
			padding: 0.75rem 0;
			opacity: 0.75;
		}

		.sx-xieyi span {
			color: #FFB300;
			padding: 0 0.15rem;
			text-decoration: underline
		}

		.sx-btn {
			width: 2.5rem;
			margin: 0 auto;
			text-align: center;
			margin-top: 0.5rem;
			opacity: 0.2;
			margin-top: 2rem;
		}
		#mobile span{
			width: 3rem;
			color: #fff;
			position: absolute;
			top: 0;
			left: 0;
			height: 100%;
			border-top-left-radius: 0.15rem;
			border-bottom-left-radius: 0.15rem;
			font-size: 0.75rem;
			background: none;
			border-right: 1px solid rgba(226, 226, 226, 0.2);
		}
		#mobile input{
			/*padding-left: 4rem;*/
		}

	</style>
</head>

<body>
	<div class="sx-content">
		<div class="sx-h" tapmode onclick="_close();">
			<img src="../i/x.png" />
		</div>
		<div class="i">
			<p>登陆</p>
			<div class="t">
				<div id="mobile">
					<input type="tel" id="mobileinput" placeholder="输入手机号码" value="" />
					<!-- <span data-quhao="86">+86</span> -->
				</div>
				<div id="yzm">
					<input type="tel" id="codeinput" placeholder="输入验证码" value="" />
					<span class="sx-login-code-c" tapmode onclick="code()">获取验证码</span>
				</div>
				<!-- <div id="yqm">
					<input type="tel" id="yqm" placeholder="邀请码（可选）" value="" />
				</div> -->
			</div>
		</div>

		<div class="sx-btn" tapmode onclick="_login();">
			<img src="../i/login_btn.png" />
		</div>

		<div class="s">
			<h4>第三方登录</h4>
			<div class="aui-card-list-footer">
				<div tapmode onclick="_loginTyle('wx')"><img src="../i/share/session.png" />
					<p>微信</p>
				</div>
				<div tapmode onclick="_loginTyle('qq')"><img src="../i/share/QFriend.png" />
					<p>QQ</p>
				</div>
				<div tapmode onclick="_loginTyle('wb')"><img src="../i/share/weibo.png" />
					<p>微博</p>
				</div>
			</div>
		</div>
		<p class="sx-xieyi">
			登陆即代表您已阅读并同意<span tapmode onclick="_page('用户协议',1)">用户协议</span>和<span tapmode onclick="_page('隐私政策',2)">隐私政策</span>
		</p>
	</div>
</body>

<script type="text/javascript" src="../j/api.js"></script>
<script type="text/javascript">
	var moduleSMSSDK;
	apiready = function() {
		if(api.systemType == 'ios'){
			$api.css($api.dom('body'),"background: url('../i/login.png') no-repeat center center;background-size: cover;");
		}
		// console.log(_config.mob)
		if (_getPrefs('_config').mob == 1) {
			moduleSMSSDK = api.require('smssdk');
		}

		$('body').css('height', api.winHeight + 'px')
		if (_getPrefs('_config').config.app == 1) {
			$('.s').addClass('aui-hide');
		}
		api.parseTapmode();
		$api.fixStatusBar($api.dom('.sx-h'));
		// 将相应的输入框获取焦点
		setTimeout(function() {
			document.getElementById("mobileinput").focus()
		}, 500)
	}

	function yes() {
		var mobile = $('#mobile input').val();
		mobile = $api.trimAll($api.trim(mobile));
		var yzm = $('#yzm input').val();
		if (_isMobile(mobile)) {
			var _this = $('#yzm span');
			_this.css('color', '#fff');
			// if(_this.css('color') == 'rgb(255, 255, 255)'){
			// 	 console.log(_this.css('color'))
			// 	_this.addClass('shake');
			// 	setTimeout(function(){
			// 		 _this.removeClass('shake');
			// 	}, 1000);
			// }
			if (yzm && yzm.length >= 4) {
				console.log(yzm.length)
				var _this = $('.sx-btn');
				if (_this.css('opacity') == 1) {
					return;
				}
				_this.css('opacity', '1');
				_this.addClass('animated rubberBand');
				setTimeout(function() {
					_this.removeClass('animated rubberBand');
				}, 1000);
				return;
			}
		} else {
			$('#yzm span').css('color', '#999');
		}

		$('.sx-btn').css('opacity', '0.2')

	}
	$("#codeinput").bind("input propertychange", function() {
		yes();
	});
	$("#mobileinput").bind("input propertychange", function() {
		yes();
	});

	function _isMobile(phone) {
		if (!(/^1[123456789]\d{9}$/.test(phone))) {
			return false;
		} else {
			return true;
		}
	}
	var isinerval
		// 发送验证码
	function code() {
		if (times) {
			return _msg('不要重复操作')
		}
		_loading();
		var mobile = $('#mobile input').val();
		mobile = $api.trimAll($api.trim(mobile));
		if (_isMobile(mobile)) {
			if (_getPrefs('_config').mob == 1) {
				var param = {
					zone: '86',
					phoneNumber: mobile,
					tempCode: _getPrefs('_config').sdkReg_id
				};
				moduleSMSSDK.getTextCode(param, function(ret, err) {
					console.log(JSON.stringify(ret)+JSON.stringify(err));
					if (err !== null && err !== undefined && err !== '') {
						_loadingCloes();
						console.log("_config.sdkReg_id:"+_getPrefs('_config').sdkReg_id)
						// 错误消息示例：{"msg":"Template not exist.","code":484}
						_suxiangw_msg2({
							content:err.msg,
							rightTitle: '知道了',
						}, function(ret) {
							dialogBox.close({
								dialogName: 'alert'
							});
						});
						return
						console.log("Error:\n" + JSON.stringify(err));
					} else {
						_loadingCloes();
						_msg('验证码已发送')
						// 正常消息示例：{"smart":false}
						console.log("Success:\n" + JSON.stringify(ret));
						$('.sx-login-code-c').html('重新获取(120s)');
						times = 120;
						$('.sx-login-code-c').css('color', '');
						isinerval = setInterval("CountDown()", 1000);
						document.getElementById("codeinput").focus()
					}
				});
				return;
			}


			_ajax('user/code', function(ret, err) {
				_loadingCloes();
				if (ret) {
					console.log(JSON.stringify(ret));
					if (ret.ret) {
						$('.sx-login-code-c').html('重新获取(120s)');
						times = 120;
						$('.sx-login-code-c').css('color', '');
						isinerval = setInterval("CountDown()", 1000);
						document.getElementById("codeinput").focus()
					} else {
						_suxiangw_msg2({
							content:ret.err,
							rightTitle: '知道了',
						}, function(ret) {
							dialogBox.close({
								dialogName: 'alert'
							});
						});
						return
					}
				} else {
					return _alert(JSON.stringify(err));
				}
			}, {
				username: mobile,
				text: '登录'
			})

		} else {
			_msg('手机号码格式错误');
			document.getElementById("mobileinput").focus()
		}
	}
	var times = 0;

	function CountDown() {
		times--;
		if (times < 1) {
			$('.sx-login-code-c').html('获取验证码');
			$('.sx-login-code-c').css('color', '#fff');
			clearInterval(isinerval);
			return;
		}
		$('.sx-login-code-c').html('重新获取(' + times + 's)');
	}
	// 登录
	function _login() {
		var mobile = $('#mobile input').val();
		mobile = $api.trimAll($api.trim(mobile));
		var yzm = $('#yzm input').val();
		if (!mobile) {
			_msg('请输入手机号码');
			return document.getElementById("mobileinput").focus()
		}
		if (!_isMobile(mobile)) {

			_msg('手机号码格式错误');
			return document.getElementById("mobileinput").focus()

		}
		if (!yzm) {
			document.getElementById("codeinput").focus()
			return _msg('请输入验证码');
		}
		if (yzm.length < 4) {
			document.getElementById("codeinput").focus()
			return _msg('请输入正确验证码')
		}
		document.activeElement.blur();
		_loading();

		// if (_getPrefs('_config').mob == 1) {
		// 	var param = {
		// 		zone: '86',
		// 		phoneNumber: mobile,
		// 		code: yzm
		// 	};
		// 	moduleSMSSDK.commitCode(param, function(ret, err) {
		// 		if (err !== null && err !== undefined && err !== '') {
		// 			// 错误消息示例：{"msg":"Template not exist.","code":484}
		// 			return _msg("code:"+err.code+"，"+err.msg);
		// 		} else {
		// 			console.log(JSON.stringify(ret))
		// 		}
		// 	});
		// 	_loadingCloes();
		// 	return;
		// }

		_ajax('user/login', function(ret, err) {
			if (ret) {
// console.log(JSON.stringify(ret)+JSON.stringify(err))
				if (ret.ret) {
					_setPrefs('_token', ret.ret);
					// 获取用户资料
					_ajax('member/info', function(ret, err) {
						// console.log(JSON.stringify(ret)+JSON.stringify(err))
						_loadingCloes();
						if (ret) {
							if (ret.err) {

							} else {
								_setPrefs('_user', ret);
								api.execScript({
									name: 'root',
									script: 'rongyun_init()'
								});
								api.execScript({
									name: 'root',
									frameName: 'oxPlayer_frm',
									script: 'yuandu();'
								});
								_close();
							}
						}
					})

				} else {
					_loadingCloes();
					return _msg(ret.err)
				}
			} else {
				_loadingCloes();
				_alert(JSON.stringify(err))
			}
		}, {
			mobile: mobile,
			code: yzm
		})
	}
	// 第三方登陆
	function _loginTyle(type) {
		document.activeElement.blur();
		if (type == 'wx') {
			var wx = api.require('wxPlus');
			wx.isInstalled(function(ret, err) {
				if (ret.installed) {
					wx.auth({}, function(reta, erra) {
						if (reta.status) {
							wx.getToken({
								code: reta.code
							}, function(retg, errg) {
								if (retg.status) {
									wx.getUserInfo({
										accessToken: retg.accessToken,
										openId: retg.openId
									}, function(retw, errw) {
										if (retw.status) {
											if (retw.openid) {
												_loginToken({
													type: 'wxapp',
													openid: retw.openid,
													unionid: retw.unionid,
													img: retw.headimgurl,
													nickname: retw.nickname,
													info: retw
												})
											} else {
												_loadingCloes();
												_alert('openId值为空')
											}
										} else {
											_loadingCloes();
										}
									});
								} else {
									_loadingCloes();
								}
							});
						} else {
							_loadingCloes();
						}
					});
				} else {
					_loadingCloes();
					_alert('当前设备未安装微信客户端')
				}
			});
		} else if (type == 'qq') {
			var qq = api.require('QQPlus');
			qq.installed(function(ret, err) {
				if (ret.status) {
					qq.login(function(retl, errl) {
						if (retl.status) {
							//		console.log(JSON.stringify(retl))
							qq.getUserInfo(function(retg, errg) {
								var openid = retl.openId;
								if (openid && retg.status) {
									if (api.systemType == 'ios') {

									} else {
										retg.info = eval('(' + retg.info.replace(/}{/, ',') + ')');
									}
									//		console.log(retg.info);
									_loginToken({
										type: 'qqapp',
										openid: openid,
										unionid: retg.unionid,
										img: retg.info.figureurl_qq_2,
										nickname: retg.info.nickname,
										info: retg.info
									})
								} else {
									_loadingCloes();
									_alert('获取QQ资料失败')
								}
							});
						} else {
							_loadingCloes();
						}
					});
				} else {
					_loadingCloes();
					_alert('当前设备未安装QQ客户端')
				}
			});
		} else if (type == 'wb') {
			var weibo = api.require('weiboPlus');
			weibo.isInstalled(function(ret) {
				_loadingCloes();
				if (ret.status) {
					weibo.auth(function(reta, erra) {
						if (reta) {
							if (reta.status) {
								weibo.getUserInfo({
									token: reta.token,
									userId: reta.userId
								}, function(retg, errg) {
									if (retg.status) {
										var openid = retg.userInfo.id
										if (openid) {
											_loginToken({
													type: 'wbapp',
													openid: openid,
													img: retg.userInfo.avatar_hd,
													nickname: retg.userInfo.name,
													info: retg.userInfo
												})
												//	_login(openid, 'wbapp')
										} else {
											_loadingCloes();
											_alert('openId值为空')
										}
										return;
									} else {
										_loadingCloes();
									}
								});
							} else {
								_loadingCloes();
							}
						} else {
							_loadingCloes();
						}
					});
				} else {
					api.alert({
						msg: '当前设备未安装新浪微博客户端'
					});
					return
				}
			});
		}
	}

	// 封装ajax登陆获取token
	function _loginToken(data) {
		// alert(JSON.stringify(data))
		_loading()
		_ajax('user/oauth', function(ret, err) {

			if (ret) {
				if (ret.mobile) {
							_loadingCloes()
					// 跳转绑定手机
					_url({
						url: 'login_mobile',
						title: '绑定手机',
						data: data
					})
					return;
				}
				_setPrefs('_token', ret.ret);
				// 获取用户资料
				// 获取用户资料
				_ajax('member/info', function(ret, err) {
					_loadingCloes();
					if (ret) {
						if (ret.err) {

						} else {
							_setPrefs('_user', ret);
							api.execScript({
								name: 'root',
								script: 'rongyun_init()'
							});
							api.execScript({
								name: 'root',
								frameName: 'scrollVideo',
								script: 'yuandu();'
							});
							_close();
						}
					}
				})


				console.log(JSON.stringify(ret))
			} else {
						_loadingCloes()
				console.log(JSON.stringify(err))
				_alert(err)
			}
		}, data)
	}
</script>

</html>
